<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../dbcom/UserCom.php");
require_once(dirname(__FILE__) . "/../dbcom/CustomerCom.php");
require_once(dirname(__FILE__) . "/../common/Common.php");
require_once(dirname(__FILE__) . "/../dbcom/OrderCom.php");
require_once(dirname(__FILE__) . "/../dbcom/RagionCom.php");
require_once(dirname(__FILE__) . "/../dbcom/UsertypeCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ServiceCom.php");
require_once(dirname(__FILE__) . "/../dbcom/StaisCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ReservetimeCom.php");
require_once(dirname(__FILE__) . "/../dbcom/AppointCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ServiceinfoCom.php");

$usercom = new UserCom();
$customercom = new CustomerCom();
$common = new Common();
$ordercom = new OrderCom();
$ragioncom = new RagionCom();
$usertypecom = new UsertypeCom();
$servicecom = new ServiceCom();
$staiscom = new StaisCom();
$reservetimecom = new ReservetimeCom();
$appointcom = new AppointCom();
$serviceinfocom = new ServiceinfoCom();

//$userid = $common->getLogin();
//if (!isset($userid) || empty($userid)) {
//    header('Location:./../login.php');
//}

$cont = $_REQUEST['cont'];
switch ($cont) {
    case 1://删除
        if (empty($_REQUEST['appointid'])) {
            echo json_encode(array("code" => 100, "msg" => "empty appointid"));
            break;
        }
        $appointid = $_REQUEST['appointid'];
        $res = $appointcom->deleteAppoint($appointid);
        if ($res) {
            echo json_encode(array("code" => 0, "msg" => "success", "data" => $appointid));
        } else {
            echo json_encode(array("code" => 400, "msg" => "delete error"));
        }
        break;
    case 2://获取订单详情
        if (empty($_REQUEST['appointid'])) {
            echo json_encode(array("code" => 100, "msg" => "empty appointid"));
            break;
        }
        $appointid = $_REQUEST['appointid'];
        $result = array("code" => 0, "msg" => "success", "data" => array());
        $appoint = $appointcom->getAppointById($appointid);
        $customer = $customercom->getCustomerById($appoint['customer_id']);
        $result["data"]["customer"] = $customer['nickname'];
        $result["data"]["address"] = $appoint['address'];
        $result["data"]["username"] = $appoint['username'];
        $result["data"]["codeid"] = $appoint['codeid'];
        $area = $ragioncom->getRagionById($appoint['area_id']);
        $result["data"]["area"] = $area['name'];
        $street = $ragioncom->getRagionById($appoint['street_id']);
        $result["data"]["street"] = $street['name'];
        $community = $ragioncom->getRagionById($appoint['community_id']);
        $result["data"]["community"] = $community['name'];
        if ($appoint['sex'] == 1) {
            $result["data"]["sex"] = "男";
        } else {
            $result["data"]["sex"] = "女";
        }

        $result["data"]["mobile"] = $appoint['mobile'];
        $usertype = $usertypecom->getUsertypeById($appoint['usertype_id']);
        $result["data"]["usertype"] = $usertype['name'];
        $aa = $reservetimecom->getRtimeById($appoint['reservetime_id']);
        $time = date("m-d", $appoint['time']);
        $result["data"]["reservetime"] = $time . " " . $aa['info'];
        $stais = $staiscom->getStaisById($appoint['stais_id']);
        if (empty($stais)) {
            $result["data"]["stais"] = "";
            $result["data"]["staistext"] = "";
        } else {
            switch ($stais['satisfied']) {
                case 0:
                    $result["data"]["stais"] = "满意";
                    break;
                case 1:
                    $result["data"]["stais"] = "基本满意";
                    break;
                case 2:
                    $result["data"]["stais"] = "不满意";
                    break;
                default :
                    $result["data"]["stais"] = "未知";
                    break;
            }
            $result["data"]["staistext"] = $stais['des'];
        }
        $service = $servicecom->getServiceById($appoint['service_id']);
        $result["data"]["service"] = $service["name"];
        echo json_encode($result);
        break;
    case 3://确认预约

        break;
    case 4://取消预约

        break;
    case 5://删除订单
        if (empty($_REQUEST['orderid'])) {
            echo json_encode(array("code" => 100, "msg" => "empty orderid"));
            break;
        }
        $orderid = $_REQUEST['orderid'];
        $res = $ordercom->deleteOrder($orderid);
        if ($res) {
            echo json_encode(array("code" => 0, "msg" => "success", "data" => $orderid));
        } else {
            echo json_encode(array("code" => 400, "msg" => "delete error"));
        }
        break;
    case 6://查看订单
        if (empty($_REQUEST['orderid'])) {
            echo json_encode(array("code" => 100, "msg" => "empty orderid"));
            break;
        }
        $orderid = $_REQUEST['orderid'];
        $result = array("code" => 0, "msg" => "success", "data" => array());
        $order = $ordercom->getOrderById($orderid);
        if (!$order) {
            echo json_encode(array("code" => 400, "msg" => "find order fail"));
            break;
        }
        $customer = $customercom->getCustomerById($order['customer_id']);
        $result["data"]["customer"] = $customer['nickname'];
        $result["data"]["tradeno"] = $order['trade_no'];
        if ($order['is_pay'] == 1) {
            $result["data"]["status"] = "已支付";
        } else {
            $result["data"]["status"] = "未支付";
        }
        $result["data"]["createtime"] = date("Y-m-d", $order['create_time']);
        $result["data"]["totalfee"] = sprintf("%.2f", $order['totalfee'] / 100);
        $result["data"]["servicelist"] = '<table class="table"> <thead><tr><th>服务名称</th><th>数量</th><th>价格(分)</th></tr> </thead><tbody>';
        $servicearr = $serviceinfocom->getServiceinfoByOrderid($orderid);
        foreach ($servicearr as $k1 => $v1) {
            $number = 0;
            $name = "";
            $fee = 0;
            $service = $servicecom->getServiceById($v1['service_id']);
            $number = $v1['total'];
            $name = $service['name'];
            if ($v1['isncard'] == 1) {
                $fee = $service['fee'];
                $fee = sprintf("%.2f", $fee / 100);
            } else {
                $fee = $service['fee'] * $number;
                $fee = sprintf("%.2f", $fee / 100);
            }
            $result["data"]["servicelist"] .= "<tr><td>" . $name . "</td><td>" . $number . "</td><td>" . $fee . "</td></tr>";
        }
        $result["data"]["servicelist"] .= "</tbody></table>";
        echo json_encode($result);
        break;
    default :
        echo json_encode(array("code" => 500, "msg" => "undefined error"));
        break;
}
exit();
